{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# brew install graphviz\n",
    "# pip install graphviz\n",
    "from graphviz import Digraph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from micrograd.engine import Value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def trace(root):\n",
    "    nodes, edges = set(), set()\n",
    "    def build(v):\n",
    "        if v not in nodes:\n",
    "            nodes.add(v)\n",
    "            for child in v._prev:\n",
    "                edges.add((child, v))\n",
    "                build(child)\n",
    "    build(root)\n",
    "    return nodes, edges\n",
    "\n",
    "def draw_dot(root, format='svg', rankdir='LR'):\n",
    "    \"\"\"\n",
    "    format: png | svg | ...\n",
    "    rankdir: TB (top to bottom graph) | LR (left to right)\n",
    "    \"\"\"\n",
    "    assert rankdir in ['LR', 'TB']\n",
    "    nodes, edges = trace(root)\n",
    "    dot = Digraph(format=format, graph_attr={'rankdir': rankdir}) #, node_attr={'rankdir': 'TB'})\n",
    "    \n",
    "    for n in nodes:\n",
    "        dot.node(name=str(id(n)), label = \"{ data %.4f | grad %.4f }\" % (n.data, n.grad), shape='record')\n",
    "        if n._op:\n",
    "            dot.node(name=str(id(n)) + n._op, label=n._op)\n",
    "            dot.edge(str(id(n)) + n._op, str(id(n)))\n",
    "    \n",
    "    for n1, n2 in edges:\n",
    "        dot.edge(str(id(n1)), str(id(n2)) + n2._op)\n",
    "    \n",
    "    return dot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: %3 Pages: 1 -->\r\n",
       "<svg width=\"1053pt\" height=\"127pt\"\r\n",
       " viewBox=\"0.00 0.00 1052.99 127.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 123)\">\r\n",
       "<title>%3</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-123 1048.99,-123 1048.99,4 -4,4\"/>\r\n",
       "<!-- 67528400 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>67528400</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"880.994,-54.5 880.994,-90.5 1044.99,-90.5 1044.99,-54.5 880.994,-54.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"921.494\" y=\"-68.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 3.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"961.994,-54.5 961.994,-90.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1003.49\" y=\"-68.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67528400ReLU -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>67528400ReLU</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"812.497\" cy=\"-72.5\" rx=\"32.4942\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"812.497\" y=\"-68.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">ReLU</text>\r\n",
       "</g>\r\n",
       "<!-- 67528400ReLU&#45;&gt;67528400 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>67528400ReLU&#45;&gt;67528400</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M845.353,-72.5C853.031,-72.5 861.619,-72.5 870.558,-72.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"870.77,-76.0001 880.77,-72.5 870.77,-69.0001 870.77,-76.0001\"/>\r\n",
       "</g>\r\n",
       "<!-- 67527920 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>67527920</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"0,-55.5 0,-91.5 164,-91.5 164,-55.5 0,-55.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"40.5\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 2.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"81,-55.5 81,-91.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"122.5\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67528144* -->\r\n",
       "<g id=\"node7\" class=\"node\"><title>67528144*</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"227\" cy=\"-45.5\" rx=\"27\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"227\" y=\"-41.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">*</text>\r\n",
       "</g>\r\n",
       "<!-- 67527920&#45;&gt;67528144* -->\r\n",
       "<g id=\"edge7\" class=\"edge\"><title>67527920&#45;&gt;67528144*</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M164.11,-57.6184C173.506,-55.7785 182.677,-53.9827 190.958,-52.3614\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"191.779,-55.7672 200.92,-50.4108 190.434,-48.8976 191.779,-55.7672\"/>\r\n",
       "</g>\r\n",
       "<!-- 67528016 -->\r\n",
       "<g id=\"node4\" class=\"node\"><title>67528016</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"290,-82.5 290,-118.5 454,-118.5 454,-82.5 290,-82.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"330.5\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 1.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"371,-82.5 371,-118.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"412.5\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67528176+ -->\r\n",
       "<g id=\"node9\" class=\"node\"><title>67528176+</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"517\" cy=\"-72.5\" rx=\"27\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"517\" y=\"-68.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">+</text>\r\n",
       "</g>\r\n",
       "<!-- 67528016&#45;&gt;67528176+ -->\r\n",
       "<g id=\"edge6\" class=\"edge\"><title>67528016&#45;&gt;67528176+</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M454.11,-84.6184C463.506,-82.7785 472.677,-80.9827 480.958,-79.3614\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"481.779,-82.7672 490.92,-77.4108 480.434,-75.8976 481.779,-82.7672\"/>\r\n",
       "</g>\r\n",
       "<!-- 67528048 -->\r\n",
       "<g id=\"node5\" class=\"node\"><title>67528048</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"0,-0.5 0,-36.5 164,-36.5 164,-0.5 0,-0.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"40.5\" y=\"-14.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 1.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"81,-0.5 81,-36.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"122.5\" y=\"-14.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 2.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67528048&#45;&gt;67528144* -->\r\n",
       "<g id=\"edge4\" class=\"edge\"><title>67528048&#45;&gt;67528144*</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M164.11,-33.8144C173.506,-35.5886 182.677,-37.3202 190.958,-38.8837\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"190.444,-42.3484 200.92,-40.7646 191.743,-35.47 190.444,-42.3484\"/>\r\n",
       "</g>\r\n",
       "<!-- 67528144 -->\r\n",
       "<g id=\"node6\" class=\"node\"><title>67528144</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"290,-27.5 290,-63.5 454,-63.5 454,-27.5 290,-27.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"330.5\" y=\"-41.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 2.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"371,-27.5 371,-63.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"412.5\" y=\"-41.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67528144&#45;&gt;67528176+ -->\r\n",
       "<g id=\"edge5\" class=\"edge\"><title>67528144&#45;&gt;67528176+</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M454.11,-60.8144C463.506,-62.5886 472.677,-64.3202 480.958,-65.8837\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"480.444,-69.3484 490.92,-67.7646 481.743,-62.47 480.444,-69.3484\"/>\r\n",
       "</g>\r\n",
       "<!-- 67528144*&#45;&gt;67528144 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>67528144*&#45;&gt;67528144</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M254.228,-45.5C261.808,-45.5 270.54,-45.5 279.746,-45.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"279.89,-49.0001 289.89,-45.5 279.89,-42.0001 279.89,-49.0001\"/>\r\n",
       "</g>\r\n",
       "<!-- 67528176 -->\r\n",
       "<g id=\"node8\" class=\"node\"><title>67528176</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"580,-54.5 580,-90.5 744,-90.5 744,-54.5 580,-54.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"620.5\" y=\"-68.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 3.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"661,-54.5 661,-90.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"702.5\" y=\"-68.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67528176&#45;&gt;67528400ReLU -->\r\n",
       "<g id=\"edge8\" class=\"edge\"><title>67528176&#45;&gt;67528400ReLU</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M744.2,-72.5C752.957,-72.5 761.584,-72.5 769.588,-72.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"769.709,-76.0001 779.709,-72.5 769.709,-69.0001 769.709,-76.0001\"/>\r\n",
       "</g>\r\n",
       "<!-- 67528176+&#45;&gt;67528176 -->\r\n",
       "<g id=\"edge3\" class=\"edge\"><title>67528176+&#45;&gt;67528176</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M544.228,-72.5C551.808,-72.5 560.54,-72.5 569.746,-72.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"569.89,-76.0001 579.89,-72.5 569.89,-69.0001 569.89,-76.0001\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.dot.Digraph at 0x4066790>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a very simple example\n",
    "x = Value(1.0)\n",
    "y = (x * 2 + 1).relu()\n",
    "y.backward()\n",
    "draw_dot(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: %3 Pages: 1 -->\r\n",
       "<svg width=\"1353pt\" height=\"210pt\"\r\n",
       " viewBox=\"0.00 0.00 1352.99 210.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 206)\">\r\n",
       "<title>%3</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-206 1348.99,-206 1348.99,4 -4,4\"/>\r\n",
       "<!-- 66630672 -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>66630672</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"587.5,-55.5 587.5,-91.5 751.5,-91.5 751.5,-55.5 587.5,-55.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"628\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.2355</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"668.5,-55.5 668.5,-91.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"710\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 66630608+ -->\r\n",
       "<g id=\"node15\" class=\"node\"><title>66630608+</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"817\" cy=\"-100.5\" rx=\"27\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"817\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">+</text>\r\n",
       "</g>\r\n",
       "<!-- 66630672&#45;&gt;66630608+ -->\r\n",
       "<g id=\"edge14\" class=\"edge\"><title>66630672&#45;&gt;66630608+</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M751.758,-88.5788C761.958,-90.4715 771.931,-92.3222 780.861,-93.9794\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"780.346,-97.4435 790.817,-95.8268 781.623,-90.561 780.346,-97.4435\"/>\r\n",
       "</g>\r\n",
       "<!-- 66630672+ -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>66630672+</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"522\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"522\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">+</text>\r\n",
       "</g>\r\n",
       "<!-- 66630672+&#45;&gt;66630672 -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>66630672+&#45;&gt;66630672</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M549.012,-73.5C557.227,-73.5 566.821,-73.5 576.936,-73.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"577.192,-77.0001 587.192,-73.5 577.192,-70.0001 577.192,-77.0001\"/>\r\n",
       "</g>\r\n",
       "<!-- 66631824 -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>66631824</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"292.5,-55.5 292.5,-91.5 456.5,-91.5 456.5,-55.5 292.5,-55.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"333\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.2355</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"373.5,-55.5 373.5,-91.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"415\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 66631824&#45;&gt;66630672+ -->\r\n",
       "<g id=\"edge6\" class=\"edge\"><title>66631824&#45;&gt;66630672+</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M456.758,-73.5C466.569,-73.5 476.171,-73.5 484.836,-73.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"484.893,-77.0001 494.893,-73.5 484.893,-70.0001 484.893,-77.0001\"/>\r\n",
       "</g>\r\n",
       "<!-- 66631824* -->\r\n",
       "<g id=\"node4\" class=\"node\"><title>66631824*</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"227\" cy=\"-73.5\" rx=\"27\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"227\" y=\"-69.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">*</text>\r\n",
       "</g>\r\n",
       "<!-- 66631824*&#45;&gt;66631824 -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>66631824*&#45;&gt;66631824</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M254.012,-73.5C262.227,-73.5 271.821,-73.5 281.936,-73.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"282.192,-77.0001 292.192,-73.5 282.192,-70.0001 282.192,-77.0001\"/>\r\n",
       "</g>\r\n",
       "<!-- 66630832 -->\r\n",
       "<g id=\"node5\" class=\"node\"><title>66630832</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"1180.99,-82.5 1180.99,-118.5 1344.99,-118.5 1344.99,-82.5 1180.99,-82.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1221.49\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.1024</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"1261.99,-82.5 1261.99,-118.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1303.49\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 66630832ReLU -->\r\n",
       "<g id=\"node6\" class=\"node\"><title>66630832ReLU</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"1112.5\" cy=\"-100.5\" rx=\"32.4942\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1112.5\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">ReLU</text>\r\n",
       "</g>\r\n",
       "<!-- 66630832ReLU&#45;&gt;66630832 -->\r\n",
       "<g id=\"edge3\" class=\"edge\"><title>66630832ReLU&#45;&gt;66630832</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1145.35,-100.5C1153.03,-100.5 1161.62,-100.5 1170.56,-100.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1170.77,-104 1180.77,-100.5 1170.77,-97.0001 1170.77,-104\"/>\r\n",
       "</g>\r\n",
       "<!-- 67529904 -->\r\n",
       "<g id=\"node7\" class=\"node\"><title>67529904</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"292.5,-0.5 292.5,-36.5 456.5,-36.5 456.5,-0.5 292.5,-0.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"333\" y=\"-14.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"373.5,-0.5 373.5,-36.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"415\" y=\"-14.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67529904&#45;&gt;66630672+ -->\r\n",
       "<g id=\"edge10\" class=\"edge\"><title>67529904&#45;&gt;66630672+</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M434.187,-36.5623C442.568,-39.4063 451.041,-42.4269 459,-45.5 469.437,-49.5297 480.639,-54.411 490.611,-58.9618\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"489.185,-62.1584 499.73,-63.1852 492.126,-55.8066 489.185,-62.1584\"/>\r\n",
       "</g>\r\n",
       "<!-- 67530448 -->\r\n",
       "<g id=\"node8\" class=\"node\"><title>67530448</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"0,-83.5 0,-119.5 164,-119.5 164,-83.5 0,-83.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"40.5\" y=\"-97.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 1.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"81,-83.5 81,-119.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"122.5\" y=\"-97.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.2355</text>\r\n",
       "</g>\r\n",
       "<!-- 67530448&#45;&gt;66631824* -->\r\n",
       "<g id=\"edge13\" class=\"edge\"><title>67530448&#45;&gt;66631824*</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M164.11,-85.6184C173.506,-83.7785 182.677,-81.9827 190.958,-80.3614\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"191.779,-83.7672 200.92,-78.4108 190.434,-76.8976 191.779,-83.7672\"/>\r\n",
       "</g>\r\n",
       "<!-- 66631888 -->\r\n",
       "<g id=\"node9\" class=\"node\"><title>66631888</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"585,-110.5 585,-146.5 754,-146.5 754,-110.5 585,-110.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"628\" y=\"-124.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;0.1331</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"671,-110.5 671,-146.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"712.5\" y=\"-124.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 66631888&#45;&gt;66630608+ -->\r\n",
       "<g id=\"edge11\" class=\"edge\"><title>66631888&#45;&gt;66630608+</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M754.295,-112.375C763.622,-110.58 772.702,-108.832 780.901,-107.254\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"781.609,-110.682 790.767,-105.356 780.286,-103.809 781.609,-110.682\"/>\r\n",
       "</g>\r\n",
       "<!-- 66631888* -->\r\n",
       "<g id=\"node10\" class=\"node\"><title>66631888*</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"522\" cy=\"-128.5\" rx=\"27\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"522\" y=\"-124.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">*</text>\r\n",
       "</g>\r\n",
       "<!-- 66631888*&#45;&gt;66631888 -->\r\n",
       "<g id=\"edge4\" class=\"edge\"><title>66631888*&#45;&gt;66631888</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M549.012,-128.5C556.576,-128.5 565.308,-128.5 574.54,-128.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"574.722,-132 584.722,-128.5 574.722,-125 574.722,-132\"/>\r\n",
       "</g>\r\n",
       "<!-- 67529968 -->\r\n",
       "<g id=\"node11\" class=\"node\"><title>67529968</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"0,-28.5 0,-64.5 164,-64.5 164,-28.5 0,-28.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"40.5\" y=\"-42.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.2355</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"81,-28.5 81,-64.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"122.5\" y=\"-42.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67529968&#45;&gt;66631824* -->\r\n",
       "<g id=\"edge12\" class=\"edge\"><title>67529968&#45;&gt;66631824*</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M164.11,-61.8144C173.506,-63.5886 182.677,-65.3202 190.958,-66.8837\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"190.444,-70.3484 200.92,-68.7646 191.743,-63.47 190.444,-70.3484\"/>\r\n",
       "</g>\r\n",
       "<!-- 67529488 -->\r\n",
       "<g id=\"node12\" class=\"node\"><title>67529488</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"290,-165.5 290,-201.5 459,-201.5 459,-165.5 290,-165.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"333\" y=\"-179.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data &#45;2.0000</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"376,-165.5 376,-201.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"417.5\" y=\"-179.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 0.0665</text>\r\n",
       "</g>\r\n",
       "<!-- 67529488&#45;&gt;66631888* -->\r\n",
       "<g id=\"edge7\" class=\"edge\"><title>67529488&#45;&gt;66631888*</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M434.187,-165.438C442.568,-162.594 451.041,-159.573 459,-156.5 469.437,-152.47 480.639,-147.589 490.611,-143.038\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"492.126,-146.193 499.73,-138.815 489.185,-139.842 492.126,-146.193\"/>\r\n",
       "</g>\r\n",
       "<!-- 67481488 -->\r\n",
       "<g id=\"node13\" class=\"node\"><title>67481488</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"290.5,-110.5 290.5,-146.5 458.5,-146.5 458.5,-110.5 290.5,-110.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"331\" y=\"-124.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.0665</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"371.5,-110.5 371.5,-146.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"415\" y=\"-124.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad &#45;2.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 67481488&#45;&gt;66631888* -->\r\n",
       "<g id=\"edge9\" class=\"edge\"><title>67481488&#45;&gt;66631888*</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M458.873,-128.5C467.968,-128.5 476.836,-128.5 484.893,-128.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"484.968,-132 494.968,-128.5 484.968,-125 484.968,-132\"/>\r\n",
       "</g>\r\n",
       "<!-- 66630608 -->\r\n",
       "<g id=\"node14\" class=\"node\"><title>66630608</title>\r\n",
       "<polygon fill=\"none\" stroke=\"black\" points=\"880,-82.5 880,-118.5 1044,-118.5 1044,-82.5 880,-82.5\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"920.5\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">data 0.1024</text>\r\n",
       "<polyline fill=\"none\" stroke=\"black\" points=\"961,-82.5 961,-118.5 \"/>\r\n",
       "<text text-anchor=\"middle\" x=\"1002.5\" y=\"-96.8\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">grad 1.0000</text>\r\n",
       "</g>\r\n",
       "<!-- 66630608&#45;&gt;66630832ReLU -->\r\n",
       "<g id=\"edge8\" class=\"edge\"><title>66630608&#45;&gt;66630832ReLU</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M1044.2,-100.5C1052.96,-100.5 1061.58,-100.5 1069.59,-100.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"1069.71,-104 1079.71,-100.5 1069.71,-97.0001 1069.71,-104\"/>\r\n",
       "</g>\r\n",
       "<!-- 66630608+&#45;&gt;66630608 -->\r\n",
       "<g id=\"edge5\" class=\"edge\"><title>66630608+&#45;&gt;66630608</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M844.228,-100.5C851.808,-100.5 860.54,-100.5 869.746,-100.5\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"869.89,-104 879.89,-100.5 869.89,-97.0001 869.89,-104\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.dot.Digraph at 0x406c0b0>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a simple 2D neuron\n",
    "import random\n",
    "from micrograd import nn\n",
    "\n",
    "random.seed(1337)\n",
    "n = nn.Neuron(2)\n",
    "x = [Value(1.0), Value(-2.0)]\n",
    "y = n(x)\n",
    "y.backward()\n",
    "\n",
    "dot = draw_dot(y)\n",
    "dot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'gout.svg'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dot.render('gout')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
